package com.zhao.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zhao.entity.User;
import com.zhao.service.UserService;
import com.zhao.utils.EncryptUtil;
import com.zhao.utils.ReturnUtils;

public class UserAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	/**
	 * 原密码
	 */
	private String oldpassword;
	
	/**
	 * 新密码
	 */
	private String newpassword;

	@Autowired
	private UserService userService;
	
	/**
	 * 用户登录
	 * @return
	 */
	public void userLogin() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		HttpServletResponse response = ServletActionContext.getResponse();
		User confirm = userService.userLogin(user);
		PrintWriter writer;
		Map<String, Object> map = new HashMap<String,Object>();
		//登录成功放入session中
		if (confirm != null) {
			session.put("user", confirm);
			map.put("status", 1);
		} else {
			map.put("status", 0);
		}
		try {
			writer = response.getWriter();
			JSON json = JSONObject.fromObject(map);
			writer.print(json.toString());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 登录成功重定向
	 * @return
	 */
	public String loginSuccess() {
		return SUCCESS;
	}
	
	/**
	 * 跳转修改密码页
	 */
	public String goChangePassword() {
		return "goChangePassword";
	}
	
	/**
	 * 修改密码
	 */
	public void changePassword() {
		User user = (User) request.getSession().getAttribute("user");
		Map<String, Object> map = new HashMap<String, Object>();
		String dePass = EncryptUtil.SHA1(oldpassword);
		//判断原密码是否正确
		if (!dePass.equals(user.getPassword())) {
			map.put("passwordError", true);
		}else {
			userService.changePassword(user, EncryptUtil.SHA1(newpassword));
			if (user != null) {
				request.getSession().removeAttribute("user");
			}
			map.put("changeSuccess", true);
		}
		ReturnUtils.responseJson(response, JSONObject.fromObject(map));
	}
	
	public String logout() {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
			session.removeAttribute("user");
		}
		return "logout";
	}
	
	/**
	 * 修改密码成功跳转
	 * @return
	 */
	public String changePasswordSuccess() {
		return "changePasswordSuccess";
	}
	
	public String personCenter() {
		//获取用户信息
		user = (User) request.getSession().getAttribute("user");
		return "personCenter";
	}
	
	/**
	 * 去登录页
	 * @return
	 */
	public String goLogin(){
		return "login";
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String getOldpassword() {
		return oldpassword;
	}

	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}
